package com.example.hellodemo.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.hellodemo.R;


public class CustomDialog extends Dialog {

    private TextView titleView, messageView,cancleView,sureView;

    private String title;
    private String message;
    private String cancleText;
    private String suerText;
    private CancleLister cancleLister;
    private ConfirmLister confirmLister;

    public CustomDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CustomDialog setMessage(String message) {
        this.message = message;
        return this;
    }



    public CustomDialog setCancleLister(String cancleText, CancleLister cancleLister) {
        this.cancleText = cancleText;
        this.cancleLister = cancleLister;
        return this;
    }

    public CustomDialog setConfirmLister(String suerText, ConfirmLister confirmLister) {
        this.suerText = suerText;
        this.confirmLister = confirmLister;
        return  this;
    }

    public CustomDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);

        WindowManager m = getWindow().getWindowManager();

        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int)(size.x * 0.8);// 设置dialog的宽度 是手机屏幕的 0.8倍
        getWindow().setAttributes(p);

        titleView = findViewById(R.id.alert_title);
        messageView = findViewById(R.id.alert_message);
        cancleView = findViewById(R.id.tv_cancle);
        sureView = findViewById(R.id.tv_sure);

        if (!title.isEmpty()){
            titleView.setText(title);
        }
        if (!message.isEmpty()){
            titleView.setText(message);
        }
        if (!cancleText.isEmpty()){
            cancleView.setText(cancleText);
        }
        if (!suerText.isEmpty()){
            sureView.setText(suerText);
        }

        cancleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancleLister != null){
                    cancleLister.onClickCancle(CustomDialog.this);
                }
            }
        });
        sureView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (confirmLister != null){
                    confirmLister.onClickConfirm(CustomDialog.this);
                }
            }
        });

    }

    public interface  CancleLister{
        public  void onClickCancle(CustomDialog dialog);

    }

    public interface  ConfirmLister{
        public  void onClickConfirm(CustomDialog dialog);
    }

}
